package com.pingan.haofang.searchcloud.monitor.dao.specs;

import com.pingan.haofang.searchcloud.monitor.domain.DashboardApiSummaryInfo;
import com.pingan.haofang.searchcloud.monitor.dto.DashboardApiSummaryInfoQueryDto;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

public class DashboardApiSummaryInfoSpecs {

    public static Specification<DashboardApiSummaryInfo> listQuerySpec(DashboardApiSummaryInfoQueryDto dto) {

        return (root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();

            if (dto.getProjectId() != null) {
                predicates.add(builder.equal(root.get("projectId"), dto.getProjectId()));
            }

            if (dto.getIndexId() != null) {
                predicates.add(builder.equal(root.get("indexId"), dto.getIndexId()));
            }

            if (dto.getStartDate() != null && dto.getEndDate() != null) {
                predicates.add(builder.between(root.get("startDate"), dto.getStartDate(), dto.getEndDate()));
            }

            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }
}
